home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 2.iso
/
STUTTGART
/
LANG
/
ASSEMBLER
/
FLOATENCODE
/
<tarZ$use>
/
files
/
floating2
/
Article3
next >
Wrap
Text File
|
1989-05-14
|
8KB
|
397 lines
Getting the Point......in BASIC
(c) Copyright TJ Chappell April 1989.
Acorn's latest version of BBC BASIC has improved many of
the features of previous versions and added many new
facilities. However, the latest version is still limited by a
maximum of 9 decimal places and maximum exponent of only 38.
These limits are normally acceptable for most users but what do
you do if you require more accuracy or a wider range of
numbers?
Until now the only possibilities were to use a scientific
calculator and enter the numbers into PRINT statements or write
a machine code program to use the facilities of the floating
point unit. Neither of these options offer a complete solution
to the BASIC programmer.
The solution offered here removes the need to learn how to
program the floating point unit directly or press any buttons
on a calculator. The program is presented as a function
library which can be installed into the machine in the normal
way using the INSTALL, LIBRARY or APPEND command as required.
It also relies upon the floating point emulator being installed
as well.
Since BASIC would round numerical variables to suit its
Page 1
own precision it is necessary that all extended accuracy
numbers are stored in string variables. The numbers are all
stored in the following format :
+/-m.mmmmmmmmmmmmmmmmmm E+/-eeee
This leads to numbers with a precision of 18 decimal
places and a maximum exponent of 9999 (somewhat larger range
than normal BASIC and most scientific calculators).
All variables to be used by the system must observe the
above format and may be initialised to a starting value using
the FNCONST routine, e.g.
number$=FNCONST("1.234567")
value$=FNCONST("3.101E69")
old$=FNCONST(STR$(old))
The FNCONST routine will accept numbers in any of the
above forms. Note that in the last example, old$ will contain
a maximum of 10 significant figures since that is the limit
imposed by BASIC.
Having initialised a variable it is then possible to use
any of the commands provided by the library. The commands are
summarised in table 1 and comprise two main forms. The first
type require only one string variable to be passed to them.
Examples of these include :
Page 2
neg$=FNNEG(pos$):REM neg=-pos
sinangle$=FNSIN(angle$):REM sinangle=SIN(angle)
inta$=FNINT(a$):REM inta=INT(a)
It is possible to include constants that have not been
created using FNCONST as long as the format described above is
used. The following statements will achieve exactly the same
result:
pi$=FNACS(FNNEG(FNCONST("1")))
pi$=FNACS("-1")
Note from the above example that it is perfectly possible
to nest the routines, ie the result of one routine being passed
directly to another. There is no limit imposed upon the
complexity of a structure except the normal 250 character line
length.
The second type of instructions require two variables and
perform the normal mathematical processes such as addition and
multiplication and other more complicated functions such as
calculating polar angles from rectangular coordinates. They are
used in the following way :
aplusb$=FNADD(a$,b$):REM aplusb=a+b
angle$=FNPOL(x$,y$):REM angle=ATN(y/x)
Page 3
Since all the numbers are handled as normal strings then
all the normal BASIC commands may be used. This includes
PRINT, LEFT$, RIGHT$ and MID$.
Two other important facilities are provided by the
routines. These are FNamDP and FNSUIT. FNamDP allows the user
to set the number of decimal places to be returned (and value
between 0 and 18) by the routines. It is used in the following
way :
past=FNamDP(14):REM Sets no. of decimal places to 14 and
returns with past set to previous
value.
The routine sets the default number of decimal places to
18. The second function FNSUIT is provided for convenience.
Since all the numbers are stored in a string form it is
perfectly acceptable to use the VAL instruction to determine
the value of a string. However, because of the extended
accuracy and range, some numbers will be unsuitable for
conversion to BASIC floating point variables and will result in
a 'Number too large' error. To allow you to determine if a
number is suitable for conversion the FNSUIT command is
provided. It will return a TRUE value if the number can be
expressed by a BASIC variable and FALSE if not. It is used in
the following way :
IF FNSUIT(extnum$) THEN a=VAL(extnum$) ELSE a=0
Page 4
Note the use of a floating point basic variable and not an
integer one which would cause rounding to occur if the number
has a fraction part.
When using the package it is best to use a mixture of
extended precision numbers and normal BASIC variables where
possible since the calculations with extended precision numbers
are fairly slow due to the complex encoding and decoding that
occurs with every command. It is advised that they only be
used where strictly necessary (ie when extended precision or
range is required). Also, only use the minimum number of
decimal places required since extra decimal places require
extra processing and so slow the package unnecessarily.
Page 5
Table 1. Routines provided and their functions.
Routine name Function
====================================================
num$=FNCONST("123") | num$="123"
num$=FNNEG(num$) | num$=-num$
num$=FNABS(num$) | num$=ABS(num$)
num$=FNINT(num$) | num$=INT(num$)
num$=FNSQR(num$) | num$=SQR(num$)
num$=FNLOG(num$) | num$=LOG(num$)
num$=FNLN(num$) | num$=LN(num$)
num$=FNEXP(num$) | num$=EXP(num$)
num$=FNSIN(num$) | num$=SIN(num$)
num$=FNCOS(num$) | num$=COS(num$)
num$=FNTAN(num$) | num$=TAN(num$)
num$=FNASN(num$) | num$=ASN(num$)
num$=FNACS(num$) | num$=ACS(num$)
num$=FNATN(num$) | num$=ATN(num$)
num$=FNADD(a$,b$) | num$=a$+b$
num$=FNMUL(a$,b$) | num$=a$*b$
num$=FNSUB(a$,b$) | num$=a$-b$
num$=FNDIV(a$,b$) | num$=a$/b$
num$=FNPOW(a$,b$) | num$=a$^b$
num$=FNMOD(a$,b$) | num$=a$ MOD b$
num$=FNPOL(a$,b$) | num$=ATN(b$/a$)
FNamDP(new) | sets no. of dec. places
FNSUIT(num$) | TRUE if BASIC can express it
Page 6